package com.gopay.bis.dao.bankorder.impl;

import com.gopay.bis.dao.bankorder.BankContractInfoDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.domain.bis.BisBankContractInfo;
import com.gopay.common.util.DateUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 银行用户签约信息DAOImpl
 *
 * @author wangxintao
 * @BankContractInfoDAO.java @2016-05-17 下午11:29:06 www.gopay.com.cn Inc.All
 *                           rights reserved.
 */
@Repository(value = "bankContractInfoDAO")
public class BankContractInfoDAOImpl extends BaseDAOImpl<BisBankContractInfo, String>implements BankContractInfoDAO {
    @Override
    public boolean isContracted(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractInfo where bankCode=:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractInfo.getBankCode());
        param.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        param.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end

        List<BisBankContractInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0 && "01".equals(infoQuerys.get(0).getContractState())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public BisBankContractInfo getContractInfo(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractInfo where bankCode=:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractInfo.getBankCode());
        param.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        param.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end

        List<BisBankContractInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0) {
            return infoQuerys.get(0);
        } else {
            return null;
        }
    }
	
	

    @Override
    public int updateBisBankContractInfo(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractDate = :contractDate, ");
        hql.append("contractExpiryDate = :contractExpiryDate, ");
        hql.append("contractState = :contractState, ");
        hql.append("contractNumber = :contractNumber, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where bankCode =:bankCode ");
        hql.append("and bankBizProdCode =:bankBizProdCode ");
        hql.append("and intrfaceChannelCode =:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractDate", DateUtils.getSystemDate());
        params.put("contractExpiryDate", contractInfo.getContractExpiryDate());
        params.put("contractState", contractInfo.getContractState());
        params.put("contractNumber", contractInfo.getContractNumber());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public int updateContractStateByCardNo(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where bankCode =:bankCode ");
        hql.append("and bankBizProdCode =:bankBizProdCode ");
        hql.append("and intrfaceChannelCode =:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public void insertBisBankContractInfo(BisBankContractInfo contractInfo) {
        Date curDate = DateUtils.getSystemDate();
        contractInfo.setContractDate(curDate);
        contractInfo.setCreatedTs(curDate);
        contractInfo.setLastUpdTs(curDate);
        this.getHibernateTemplate().save(contractInfo);
    }

    @Override
    public BisBankContractInfo getContractInfoByCard(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractInfo where bankCode=:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractInfo.getBankCode());
        param.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        param.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end
        List<BisBankContractInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0) {
            return infoQuerys.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int updateContractInfoAndElements(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs, ");
        hql.append("contractNumber = :contractNumber,");
        hql.append("verificationCode = :verificationCode ");
        hql.append("where bankCode = :bankCode ");
        hql.append("and cardNo in (:cardNo) ");
        hql.append("and intrfaceChannelCode = :intrfaceChannelCode ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("contractNumber", contractInfo.getContractNumber());
        params.put("verificationCode", contractInfo.getVerificationCode());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> cardNoList = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", cardNoList);
        // edit hougn 2017-04-17 央行检测敏感信息整改  end

        return this.updateByHql(hql.toString(), params);
    }

	@Override
	public int updateContractInfo(BisBankContractInfo contractInfo) {
		StringBuffer hql = new StringBuffer();
		hql.append("update BisBankContractInfo set ");
		hql.append("contractState = :contractState, ");
		hql.append("lastUpdTs = :lastUpdTs, ");
		hql.append("contractNumber = :contractNumber,");
		hql.append("verificationCode = :verificationCode,");
		// edit hougn 2017-04-17 央行检测敏感信息整改  start
		hql.append("bindMobile in (:bindMobile) ");
		hql.append("where bankCode =:bankCode ");
		hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("contractNumber", contractInfo.getContractNumber());
        params.put("verificationCode", contractInfo.getVerificationCode());
        List<String> bindMobileList = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getBindMobile());
        params.put("bindMobile", bindMobileList);
        params.put("bankCode", contractInfo.getBankCode());
        List<String> cardNoList = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", cardNoList);
        // edit hougn 2017-04-17 央行检测敏感信息整改  end

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public int updateContractState(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where bankCode =:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end
        return this.updateByHql(hql.toString(), params);
    }
    
    @Override
    public int updateContractStateBySingNo(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where bankCode =:bankCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        hql.append("and contractNumber=:contractNumber");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        params.put("contractNumber", contractInfo.getContractNumber());

        return this.updateByHql(hql.toString(), params);
    }


    @Override
    public BisBankContractInfo getSignNoByCard(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractInfo where bankCode=:bankCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        hql.append("and cardNo in (:cardNo) ");
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractInfo.getBankCode());
        param.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        param.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证  end
        List<BisBankContractInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0) {
            return infoQuerys.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int updateContractInfoByProdChannelCode(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs, ");
        hql.append("contractNumber = :contractNumber ");
        if (null != contractInfo.getVerificationCode()) {
            hql.append(", verificationCode = :verificationCode ");
        }
        hql.append("where bankCode =:bankCode ");
        hql.append("and bankBizProdCode =:bankBizProdCode ");
        hql.append("and intrfaceChannelCode =:intrfaceChannelCode ");
        // edit hougn 2017-02-28 pci认证 start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractInfo.getContractState());
        if (null != contractInfo.getVerificationCode()) {
            params.put("verificationCode", contractInfo.getVerificationCode());
        }
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("contractNumber", contractInfo.getContractNumber());
        // params.put("verificationCode", contractInfo.getVerificationCode());
        params.put("bankCode", contractInfo.getBankCode());
        params.put("bankBizProdCode", contractInfo.getBankBizProdCode());
        params.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        List<String> list = SensitiveInfoUtils.getAllHisCipherText(contractInfo.getCardNo());
        params.put("cardNo", list);
        // edit hougn 2017-02-28 pci认证 end

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public BisBankContractInfo getContractInfoBySingNo(BisBankContractInfo contractInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractInfo where bankCode=:bankCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        hql.append(" abd contractNumber=:contractNumber ");
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractInfo.getBankCode());
        param.put("intrfaceChannelCode", contractInfo.getIntrfaceChannelCode());
        param.put("contractNumber", contractInfo.getContractNumber());
        // edit hougn 2017-02-28 pci认证  end
        List<BisBankContractInfo> infoQuerys = this.findByHql(hql.toString(), param);
        if (infoQuerys != null && infoQuerys.size() != 0) {
            return infoQuerys.get(0);
        } else {
            return null;
        }
    }
}
